package org.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.reggie.common.R;
import org.example.reggie.dto.DishDto;
import org.example.reggie.dto.SetmealDto;
import org.example.reggie.entity.Category;
import org.example.reggie.entity.Dish;
import org.example.reggie.entity.Setmeal;
import org.example.reggie.service.CategoryService;
import org.example.reggie.service.SetMealDishService;
import org.example.reggie.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    @Autowired
    private SetMealDishService setMealDishService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
//        log.info("{}", setmealDto.toString());
        setMealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoInfo = new Page<>();
        LambdaQueryWrapper<Setmeal> lq = new LambdaQueryWrapper<>();
        lq.like(name != null, Setmeal::getName, name);
        lq.orderByDesc(Setmeal::getUpdateTime);
        setMealService.page(pageInfo, lq);

        BeanUtils.copyProperties(pageInfo, dtoInfo, "records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map(item -> {
            SetmealDto dto = new SetmealDto();
            BeanUtils.copyProperties(item, dto);
            Long id = item.getCategoryId();
            Category category = categoryService.getById(id);
            if (category != null) {
                String cname = category.getName();
                dto.setCategoryName(cname);
            }
            return dto;
        }).collect(Collectors.toList());
        dtoInfo.setRecords(list);

        return R.success(dtoInfo);
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        setMealService.deleteWithDish(ids);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> lq = new LambdaQueryWrapper<>();
        lq.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        lq.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        lq.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setMealService.list(lq);
        return R.success(list);
    }
}
